package com.martin.lib_base.ui.theme

import android.app.Activity
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Density
import androidx.core.view.WindowCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.coroutineScope
import com.martin.lib_base.R
import com.martin.lib_base.interfaces.IBindLifecycle
import com.martin.lib_base.ktx.loge

/**
 * 浅色主题
 */
private val LightColorScheme = lightColorScheme(
    primary = ColorPrimary,
    secondary = ColorSecondary,
    tertiary = ColorTertiary,
)

@Composable
fun CommonTheme(
    lifecycleObserver: LifecycleObserver?,
    content: @Composable BoxScope.() -> Unit
) {

    val fontScale = LocalDensity.current.fontScale
    val displayMetrics = LocalContext.current.resources.displayMetrics
    val widthPixels = displayMetrics.widthPixels

    MaterialTheme(
        colorScheme = LightColorScheme,
        typography = Typography,
        content = {
            CompositionLocalProvider(
                LocalDensity provides Density(
                    // 设置屏幕尺寸为360
                    density = widthPixels / 360.0f,
                    fontScale = fontScale
                )
            ) {
                Box(modifier = Modifier.fillMaxSize()) {
                    content()
                }
            }
        },
    )

    val lifecycle = LocalLifecycleOwner.current.lifecycle
    LaunchedEffect(lifecycleObserver) {
        lifecycleObserver?.let { lifecycle.addObserver(it) }
    }
}